package com.doangeometrydash.scene;

import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import com.doangeometrydash.base.BaseScene;
import com.doangeometrydash.manager.SceneManager;
import com.doangeometrydash.manager.SceneManager.SceneType;

public class MainMenuScene extends BaseScene implements IOnMenuItemClickListener
{
	private MenuScene menuChildScene;
	private final int MENU_PLAY = 0;
	private final int MENU_OPTIONS = 1;
	private final int MENU_INFORMATION = 2;

	@Override
	public void createScene()
	{
		this.createBackground();
		this.createMenuChildScene();
	}

	private void createBackground()
	{
		this.attachChild(new Sprite(240, 160, mResourcesManager.menu_background_region, vbom));
	}

	public void createMenuChildScene()
	{
		menuChildScene = new MenuScene(mCamera);

		final IMenuItem playMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_PLAY, mResourcesManager.play_region, vbom), 1.2f, 1);
		final IMenuItem optionsMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_OPTIONS, mResourcesManager.options_region, vbom), 1.2f,
				1);
		final IMenuItem informationMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_INFORMATION, mResourcesManager.information_region,
				vbom), 1.2f, 1);

		menuChildScene.addMenuItem(playMenuItem);
		menuChildScene.addMenuItem(optionsMenuItem);
		menuChildScene.addMenuItem(informationMenuItem);

		playMenuItem.registerEntityModifier(new MoveModifier(1.0f, -50, 370, mCamera.getCameraSceneWidth() / 4 + 20,
				mCamera.getCameraSceneHeight() / 2));
		optionsMenuItem.registerEntityModifier(new MoveModifier(1.0f, 530, 370, mCamera.getCameraSceneWidth() - mCamera.getCameraSceneWidth() / 4
				- 20, mCamera.getCameraSceneHeight() / 2));
		informationMenuItem.registerEntityModifier(new MoveModifier(2.0f, 20, 23, 455, 23));

		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);

		menuChildScene.setOnMenuItemClickListener(this);

		setChildScene(menuChildScene);
	}

	@Override
	public void onBackKeyPressed()
	{
		mResourcesManager.storeData();
		System.exit(0);
	}

	@Override
	public SceneType getSceneType()
	{
		return SceneType.SCENE_MENU;
	}

	@Override
	public void disposeScene()
	{

	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
	{
		switch (pMenuItem.getID())
		{
		case MENU_PLAY:
			SceneManager.getInstance().loadGameScene();
			mResourcesManager.soundTrackMenu.pause();
			mResourcesManager.soundTrackMenu.seekTo(0);
			return true;
		case MENU_OPTIONS:
			SceneManager.getInstance().createOptionsMenuScene();
			return true;
		case MENU_INFORMATION:
			SceneManager.getInstance().createAboutScene();
			return true;
		default:
			return false;
		}

	}
}
